package impls.agents;

import impls.classes.Constants;
import impls.classes.Position;
import impls.graphics.ControlCenter;
import interfaces.agents.Graphics;
import jade.core.Agent;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.AMSService;
import jade.domain.FIPAAgentManagement.AMSAgentDescription;
import jade.domain.FIPAAgentManagement.SearchConstraints;
import jade.lang.acl.ACLMessage;

import java.util.HashMap;

public class GraphicsAgent extends Agent implements Graphics {

	private static final long serialVersionUID = 3664581772118310527L;

	private HashMap<String, Position> cars;

	private HashMap<String, Position> stations;

	private HashMap<String, Float> carfuel;

	private HashMap<String, Float> stationprice;

	private ControlCenter cc;

	private int noCars;

	private int noStations;

	/**
	 * setup method of CarAgent
	 */
	public void setup() {
		// initiate control center for showing screen
		cc = new ControlCenter("cc");
		cars = new HashMap<String, Position>();
		stations = new HashMap<String, Position>();

		carfuel = new HashMap<String, Float>();
		stationprice = new HashMap<String, Float>();

		noCars = 0;
		noStations = 0;

		AMSAgentDescription[] agents = null;
		try {
			SearchConstraints c = new SearchConstraints();

			c.setMaxResults(new Long(-1));
			agents = AMSService.search(this, new AMSAgentDescription(), c);
		} catch (Exception e) {
			System.out.println("Problem searching AMS: " + e);
			e.printStackTrace();
		}

		// create message with own id and send to all agents
		ACLMessage msg = new ACLMessage(ACLMessage.INFORM);
		msg.setConversationId(Constants.GRAPHICSAID);
		msg.setContent(getAID().getName().toString());

		// Sending to all Agents, except himself
		for (int i = 0; i < agents.length; i++) {
			if (agents[i].getName() != this.getAID()) {
				msg.addReceiver(agents[i].getName());
			}
		}
		send(msg);

		// addBehaviour(new RequestPosition(this, 10000));
		addBehaviour(new TickerBehaviour(this, Constants.INTERVAL) {
			private static final long serialVersionUID = 0L;

			// redraw screen every interval
			protected void onTick() {
				draw();
			}
		});

		addBehaviour(new CyclicBehaviour(this) {
			private static final long serialVersionUID = 9034535378085818088L;

			public void action() {
				ACLMessage msg = receive();
				if (msg != null) {
					// receive messages from other agents
					if (msg.getConversationId().equals(Constants.DRAW)) {
						// process message string
						String[] data = msg.getContent().split(",");
						String aid = data[0];
						String type = data[1];
						int posX = Integer.valueOf(data[2]);
						int posY = Integer.valueOf(data[3]);
						Float priceorfuel = Float.valueOf(data[4]);
						Position pos = new Position(posX, posY);
						// save car and station infos to hashmaps
						if (type.equals(Constants.CAR)) {
							cars.put(aid, pos);
							carfuel.put(aid, priceorfuel);
							cc.setCarfuel(carfuel);

							if (cars.size() != noCars) {
								cc.setupCarAgentChoice(carfuel);
								noCars = cars.size();
							}

						} else if (type.equals(Constants.STATION)) {
							stations.put(aid, pos);
							stationprice.put(aid, priceorfuel);
							cc.setStationprice(stationprice);

							if (stations.size() != noStations) {
								cc.setupStationAgentChoice(stationprice);
								noStations = stations.size();
							}

						}
					}
					if (msg.getConversationId().equals(Constants.MESSAGE)) {
						// process message string
						cc.postMessage(msg.getContent());
					}
				}
			}
		});
	}

	/**
	 * redraw whole map
	 */
	public void draw() {
		cc.buildMap(cars, stations);
	}
}
